package com.zhucan.account.web.controller;

import com.zhucan.account.application.command.AccountCommandService;
import com.zhucan.account.application.query.AccountQueryService;
import com.zhucan.account.application.query.dto.AccountBalanceDTO;
import com.zhucan.account.application.query.dto.AccountDTO;
import com.zhucan.account.application.query.dto.PageDTO;
import com.zhucan.account.application.query.qry.AccountBalanceQry;
import com.zhucan.account.application.query.qry.AccountQry;
import com.zhucan.account.application.command.cmd.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * author: zhuCan
 * date: 2020/8/4 19:54
 * description:
 */
@RestController
@RequestMapping(value = "/accounts", produces = "application/hal+json")
@Api(value = "账户模块", tags = {"账户模块"})
@AllArgsConstructor
public class AccountController {

  // 应用服务
  private AccountCommandService accountCommandService;

  // 查询服务
  private AccountQueryService queryService;

  @PostMapping
  @ApiOperation("开户")
  public ResponseEntity<?> createAccount(@RequestBody @Valid AccountCreateCommand command) {
    accountCommandService.createAccount(command);
    return ResponseEntity.status(HttpStatus.CREATED).build();
  }

  @PutMapping("/{id}")
  @ApiOperation("设置密码")
  public ResponseEntity<?> setPassword(@PathVariable Long id,
                                       @RequestBody @Valid AccountPasswordChangeCommand command) {
    accountCommandService.setPassword(id, command);
    return ResponseEntity.ok().build();
  }

  @PutMapping("/{id}/suspend")
  @ApiOperation("冻结账户")
  public ResponseEntity<?> suspend(@PathVariable Long id) {
    accountCommandService.suspendAccount(id);
    return ResponseEntity.ok().build();
  }

  @PutMapping("/{id}/deposit")
  @ApiOperation("存钱")
  public ResponseEntity<?> deposit(@PathVariable Long id,
                                   @RequestBody @Valid AccountDepositCommand command) {
    accountCommandService.deposit(id, command);
    return ResponseEntity.ok().build();
  }

  @PutMapping("/{id}/withdraw")
  @ApiOperation("取钱")
  public ResponseEntity<?> withdraw(@PathVariable Long id,
                                    @RequestBody @Valid AccountWithdrawCommand command) {
    accountCommandService.withdraw(id, command);
    return ResponseEntity.ok().build();
  }

  @PutMapping("/{id}/transfer/{targetId}")
  @ApiOperation("转账")
  public ResponseEntity<?> transfer(@PathVariable Long id,
                                    @PathVariable Long targetId,
                                    @RequestBody @Valid AccountTransferCommand command) {
    accountCommandService.transfer(id, targetId, command);
    return ResponseEntity.ok().build();
  }

  @GetMapping
  @ApiOperation("分页条件检索")
  public ResponseEntity<PageDTO<AccountDTO>> search(AccountQry qry) {
    return ResponseEntity.ok(queryService.page(qry));
  }

  @GetMapping("/balance")
  @ApiOperation("余额查询")
  public ResponseEntity<AccountBalanceDTO> balance(AccountBalanceQry qry) {
    return ResponseEntity.ok(queryService.balance(qry));
  }


}
